import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:ls_work/net/api/event_apis.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/net/response/event_manage/event_manage_list_data.dart';
import 'package:ls_work/net/response/system_info_data.dart';

class EventManageModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  ///事件列表
  Future<List<EventManageListData>> getEventList(
      {String buildingIds = "",
      String eventTypes = "",
      int page = 1,
      int rows = 20}) async {
    var map = {
      "buildingIds": buildingIds,
      "eventTypes": eventTypes,
      "page": page,
      "rows": rows
    };

    var data = await HttpGo.instance.postData(EventApis.eventList, data: map);
    if ("0" == data['code']) {
      var list = ((data['data']['rows'] as List) ?? [])
          .map((e) => EventManageListData.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }
  }

  ///创建事件
  Future<Map> createEvent(
      {required dynamic elderId,
      required dynamic eventType,
      required String occurrenceTime,
      required dynamic occurrencePlace,
      required String placeDetail,
      required String eventProcess,
      List<String>? attatchIds}) async {
    var map = {
      // "elderId": elderId,
      "eventType": eventType,
      "occurrenceTime": occurrenceTime,
      "occurrencePlace": occurrencePlace,
      "placeDetail": placeDetail,
      "eventProcess": eventProcess,
      "attatchIds": attatchIds,
    };
    if(elderId!=null){
      map["elderId"] = elderId;
    }
    var data = await HttpGo.instance.postData(EventApis.createEvent, data: map);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///创建事件处理
  Future<Map> createHandle(
      {required dynamic emergencyEventManageId,
      required String handleTime,
      required String detail,
      List<String>? attatchIds}) async {
    var map = {
      "emergencyEventManageId": emergencyEventManageId,
      "handleTime": handleTime,
      "detail": detail,
      "attatchIds": attatchIds,
    };

    var data =
        await HttpGo.instance.postData(EventApis.createHandle, data: map);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///创建跟进记录
  Future<Map> createFollow(
      {required dynamic emergencyEventManageId,
      required String handleTime,
      required String detail,
      List<String>? attatchIds,
      required bool isEndFollow}) async {
    var map = {
      "emergencyEventManageId": emergencyEventManageId,
      "handleTime": handleTime,
      "detail": detail,
      "attatchIds": attatchIds,
      "isEndFollow":isEndFollow
    };
    var data =
        await HttpGo.instance.postData(EventApis.createFollow, data: map);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }

  }

  ///获取应急号码
  Future<SystemInfoData> getSosPhone() async {
    var map = {
      "domain":"EMERGENCY.EVENT.MANAGE",
      "paramName":"EMERGENCY.EVENT.MANAGE.MOBILE"
    };
    var data = await HttpGo.instance.getData(EventApis.sysInfo,data: map);
    if("0"==data['code']){
      return SystemInfoData.fromJson(data['data']);
    }else{
      throw data['msg'];
    }
  }
}
